<?php
namespace Api\Controller;
use Think\Controller;
class AddressController extends PublicController {

    /**
     * 获取用户地址列表
     * @author tiedong
     * @date 2018-01-23
     */
    public function index(){
        $user_id = $this->get("id");
        if (!$user_id) {
            $this->json_error("请登录！");
            exit();
        }
    	//所有地址
    	$addressModel = M('address');
		$adds_list=$addressModel
                    ->field("id,name,tel,region_name,address,uid,is_default")
                    ->where('uid='.intval($user_id))
                    ->order('is_default desc,id desc')
                    ->select();
        $this->json_result($adds_list);
		exit();
    }


    /**
     * 会员添加地址
     * @author tiedong
     * @date 2018-01-23
     */
    public function add_adds(){
        $user_id = $this->get("id");
        if (!$user_id) {
            $this->json_error("请登录！");
            exit();
        }
        //查找该用户是否已经存在默认地址
        $data = array();
        $data['name'] = trim($_GET['receiver']);
        $data['tel'] = trim($_GET['tel']);
        $data['region_name'] = $_GET['region_name'];
        $data['address'] = $_GET['adds'];
        //$data['code'] = $_POST['code'];
        $data['uid'] = intval($user_id);
        if (!$data['name'] || !$data['tel'] || !$data['address']) {
            $this->json_error("请完善信息后再提交！！！");
            exit();
        }
        $check_id = M('address')->where($data)->getField('id');
        if ($check_id) {
            $this->json_error("该地址已经添加！");
            exit();
        }
        $data['is_default'] = $_GET['is_default'];
        /*$province = M('china_city')->where('id='.intval($data['sheng']))->getField('name');
        $city_name = M('china_city')->where('id='.intval($data['city']))->getField('name');
        $quyu_name = M('china_city')->where('id='.intval($data['quyu']))->getField('name');
        $data['address_xq'] = $province.' '.$city_name.' '.$quyu_name.' '.$data['address'];*/
        $res = M('address')->add($data);
        if ($res) {
            M('address')->where("id !=".$res)->setField("is_default",0);
            $this->json_result("添加地址成功！");
            exit();
        }else{
            $this->json_error("添加失败！");
            exit();
        }

    }

    /**
     * 会员获取单个地址
     * @author tiedong
     * @date 2018-01-22
     */
    public function details(){
        $addr_id = intval($_GET['addr_id']);
        if (!$addr_id) {
            $this->json_error("参数有误！");
            exit();
        }
        $address = M('address')->where('id='.intval($addr_id))->find();
        if (!$address) {
            $this->json_error("该地址不存在！");
            exit();
        }
        $arr=array();
        $arr['addr_id']=$address['id'];
        $arr['name'] = $address['name'];
        $arr['tel'] = $address['tel'];
        $arr['region_name'] = $address['region_name'];
        $arr['address'] = $address['address'];
        $arr['is_default'] = $address['is_default'];
        $this->json_result($arr);
        exit();
    }

    /**
     * 用户修改地址
     * @author tiedong
     * @date 2018-01-23
     */
    public function modifyAddress()
    {
        $user_id = $this->get("id");
        if (!$user_id) {
            $this->json_error("请登录！");
            exit();
        }
        $addr_id = $_GET['addr_id'];
        $data = array();
        $data['name'] = trim($_GET['receiver']);
        $data['tel'] = trim($_GET['tel']);
        $data['region_name'] = $_GET['region_name'];
        $data['address'] = $_GET['adds'];
        //$data['code'] = $_POST['code'];
        $data['uid'] = intval($user_id);
        if (!$data['name'] || !$data['tel'] || !$data['address']) {
            $this->json_error("请完善信息后再提交！！！");
            exit();
        }
        $data['is_default'] = intval($_GET['is_default']);
        $res = M('address')->where('id='.$addr_id)->save($data);
        if ($res) {
            M('address')->where("id !=".$addr_id)->setField("is_default",0);
            $this->json_result("修改地址成功！");
            exit();
        }else{
            $this->json_error("修改失败！");
            exit();
        }
    }

    /**
     * 用户删除地址
     * @author tiedong
     * @date 2018-01-23
     */
    public function del_adds(){
        $user_id=intval($this->get("id"));
        if (!$user_id){
            echo json_encode(array('status'=>0,'err'=>'网络异常.'.__LINE__));
            exit();
        }
        $id_arr = trim($_POST['id_arr'],',');
        if ($id_arr) {
            $res = M('address')->where('uid='.intval($user_id).' AND id IN ('.$id_arr.')')->delete();
            if ($res) {
                $this->json_result("删除成功！");
                exit();
            }else{
                $this->json_error("删除失败！");
                exit();
            }
        }else{
            $this->json_error("没有找到要删除的数据");
            exit();
        }
    }

    //***************************
    //  获取省份数据接口
    //***************************
    public function get_province(){
        //所有省份
        $china_city=M("china_city");
        $list = $china_city->where('tid=0')->field('id,name')->select();

        echo json_encode(array('status'=>1,'list'=>$list));
        exit();
    }

    //***************************
    //  获取城市数据接口
    //***************************
    public function get_city(){
        $sheng=intval($_REQUEST['sheng']);
        if (!$sheng){
            echo json_encode(array('status'=>0,'err'=>'请选择省份.'.__LINE__));
            exit();
        }
        
        //所有省份
        $china_city=M("china_city");
        $list = $china_city->where('tid=0')->field('id,name')->select();
        $city = $china_city->where('tid='.intval($list[$sheng-1]['id']))->field('id,name')->select();

        echo json_encode(array('status'=>1,'city_list'=>$city,'sheng'=>intval($list[$sheng-1]['id'])));
        exit();
    }

    //***************************
    //  获取区域数据接口
    //***************************
    public function get_area(){
        $city=intval($_REQUEST['city']);
        if (!$city){
            echo json_encode(array('status'=>0,'err'=>'请选择城市.'.__LINE__));
            exit();
        }
        
        //所有省份
        $china_city=M("china_city");
        $list = $china_city->where('tid='.intval($_REQUEST['sheng']))->field('id,name')->select();
        $area = $china_city->where('tid='.intval($list[$city-1]['id']))->field('id,name')->select();

        echo json_encode(array('status'=>1,'area_list'=>$area,'city'=>intval($list[$city-1]['id'])));
        exit();
    }

    //***************************
    //  获取邮政编号接口
    //***************************
    public function get_code(){
        $quyu=intval($_REQUEST['quyu']);
        
        //所有省份
        $china_city=M("china_city");
        $list = $china_city->where('tid='.intval($_REQUEST['city']))->field('id,name')->select();
        $code = $china_city->where('id='.intval($list[$quyu-1]['id']))->getField('code');
        echo json_encode(array('status'=>1,'code'=>$code,'area'=>intval($list[$quyu-1]['id'])));
        exit();
    }

    //***************************
    //  设置默认地址
    //***************************
    public function set_default(){
        $uid=intval($_REQUEST['uid']);
        if (!$uid){
            echo json_encode(array('status'=>0,'err'=>'登录状态异常.'));
            exit();
        }

        $addr_id = intval($_REQUEST['addr_id']);
        if (!$addr_id) {
            echo json_encode(array('status'=>0,'err'=>'地址信息错误.'));
            exit();
        }
        //修改默认状态
        $check = M('address')->where('uid='.intval($uid).' AND is_default=1')->find();
        if ($check) {
            $up1= M('address')->where('uid='.intval($uid))->save(array('is_default'=>0));
            if (!$up1) {
                echo json_encode(array('status'=>0,'err'=>'设置失败.'.__LINE__));
                exit();
            }
        }
        
        $up2 = M('address')->where('id='.intval($addr_id).' AND uid='.intval($uid))->save(array('is_default'=>1));
        if ($up2) {
            echo json_encode(array('status'=>1));
            exit();
        }else{
            echo json_encode(array('status'=>0,'err'=>'设置失败.'.__LINE__));
            exit();
        }
    }

    public function modifyDefault()
    {
        $user_id = $this->get("id");
        if (!$user_id) {
            $this->json_error("请登录！");
            exit();
        }
        $addr_id = $_GET['addr_id'];
        $result = $this->modifyData($addr_id,1);
        if ( $result ==true ) {
            M('address')->where("id !=".$addr_id)->setField("is_default",0);
            $this->json_result("设置成功！");
            exit;
        } else {
            $this->json_error("设置失败！");
            exit;
        }
    }

    public function cancelDefault()
    {
        $user_id = $this->get("id");
        if (!$user_id) {
            $this->json_error("请登录！");
            exit();
        }
        $addr_id = $_GET['addr_id'];
        $result = $this->modifyData($addr_id,0);
        if ( $result == true ) {
            $this->json_result("设置成功！");
            exit;
        } else {
            $this->json_error("设置失败！");
            exit;
        }
    }

    public function modifyData($addr_id,$type)
    {
        $result = M("address")->where("id=".$addr_id)->setField('is_default',$type);
        if ( $result ) {
            return true;
        }
        return false;
    }

}